package org.jpass.dc.common.util;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.jpass.dc.business.user.bean.UserGroupVOBean;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.business.user.logic.UserGroupLogic;
import org.jpass.dc.business.user.logic.UserGroupLogicImpl;
import org.jpass.dc.business.user.logic.UserManageLogic;
import org.jpass.dc.business.user.logic.UserManageLogicImpl;

/**
 * 自动添加用户类
 * **/
public class AutoAddUser {
	UserVOBean userVOBean = new UserVOBean();
	UserManageLogic userManageLogic = new UserManageLogicImpl();
	UserGroupLogic userGroupLogic = new UserGroupLogicImpl();

	public static void main(String[] args) {
		int count = 50; // 新建用户数量
		try {
			new AutoAddUser().autoInsertUser(count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("已新建" + count + "个用户！！");
	}

	/** 自动添加用户 **/
	public void autoInsertUser(int count) throws Exception {
		List<UserGroupVOBean> list = userGroupLogic.findAllGroup();

		for (int i = 0; i < count; i++) {
			String str = this.randomString(6);
			userVOBean.setEmail(str + "@sina.com");
			userVOBean.setUsername(str);
			userVOBean.setPassword(Encrypter.md5Encrypt("lanjie"));
			userVOBean.setGroup(list.get(0));
			userVOBean.setWorkPath("resource/" + str);
			userVOBean.setSex("男");
			userVOBean.setCreatedDate(new Date());
			userVOBean.setSendEmailTime(new Date());
			userVOBean.setPoint(0); // 初始用户积分为0
			userVOBean.setIsMember(0); // 是否为收费会员：0否；1是
			userVOBean.setIsOnline(0);// 初始为未登录
			userVOBean.setIsActivated(1);// 初始为未激活
			userVOBean.setLoginTimes(0); // 登录次数
			userManageLogic.register(userVOBean);
		}
	}

	/**
	 * 产生随机字符串
	 * */
	public String randomString(int length) {
		Random randGen = null;
		char[] numbersAndLetters = null;
		if (length < 4 || length > 18) {
			return null;
		}
		if (randGen == null) {
			randGen = new Random();
			numbersAndLetters = ("abcdefghijklmnopqrstuvwxyz"
					+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
		}
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = numbersAndLetters[randGen.nextInt(51)];
		}
		return new String(randBuffer);
	}

}
